﻿// CSV Report Generator for SQL
// Copyright (C) 2010 Perry Curt Day II

// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace CSVReportGenerator
{
    public partial class FormReportEditor : Form
    {
        private bool edit;
        public FormReportEditor(bool edit)
        {
            InitializeComponent();
            this.edit = edit;

            if (edit)
            {
                textBoxTitle.Text = FormMain.selectedReport.Title;
                labelAuthor.Text = FormMain.selectedReport.Author;
                labelCreated.Text = FormMain.selectedReport.Created.ToString();
                labelModified.Text = FormMain.selectedReport.Modified.ToString();
                textBoxDescription.Text = FormMain.selectedReport.Description;
                textBoxConnectionString.Text = FormMain.selectedReport.SqlConnectionString;
                textBoxQueryString.Text = FormMain.selectedReport.SqlQueryString;
            }
            else
            {
                textBoxTitle.Text = "New Report";
                labelAuthor.Text = string.Format("{0}\\{1}", Environment.UserDomainName, Environment.UserName);
                labelCreated.Text = string.Format("{0} ({1})", DateTime.Now, "Pending");
                labelModified.Text = string.Format("{0} ({1})", DateTime.Now, "Pending");
            }
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        private void buttonOk_Click(object sender, EventArgs e)
        {
            if (InputValidated())
            {
                UpdateReportCollection();
                this.DialogResult = DialogResult.OK;
                this.Close();
            }
        }

        private void buttonApply_Click(object sender, EventArgs e)
        {
            if (InputValidated())
            {
                UpdateReportCollection();
            }
        }

        private void UpdateReportCollection()
        {
            if (edit)
            {
                foreach (Report report in FormMain.reportCollection)
                {
                    if (report == FormMain.selectedReport)
                    {
                        report.Title = textBoxTitle.Text;
                        report.Modified = DateTime.Now;
                        report.Description = textBoxDescription.Text;
                        report.SqlConnectionString = textBoxConnectionString.Text;
                        report.SqlQueryString = textBoxQueryString.Text;
                    }
                } 
            }
            else
            {
                Report report = new Report();
                report.Title = textBoxTitle.Text;
                report.Author = labelAuthor.Text;
                report.Created = DateTime.Now;
                report.Modified = DateTime.Now;
                report.Description = textBoxDescription.Text;
                report.SqlConnectionString = textBoxConnectionString.Text;
                report.SqlQueryString = textBoxQueryString.Text;

                FormMain.reportCollection.Add(report);
            }

            FormMain.reportCollection.Sort();
        }

        private bool InputValidated()
        {
            if (textBoxTitle.Text.Equals(string.Empty))
            {
                MessageBox.Show("A title is required to save the report.",
                    "Invalid report title.....", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                return false;
            }
            else if(textBoxConnectionString.Text.Equals(string.Empty) ||
                textBoxQueryString.Text.Equals(string.Empty))
            {
                MessageBox.Show("The SQL connection and query strings must be completed to save the report.",
                    "Invalid SQL entries.....", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                return false;
            }
            else if (textBoxQueryString.Text.ToUpper().Contains("DELETE") ||
                textBoxQueryString.Text.ToUpper().Contains("INSERT") ||
                textBoxQueryString.Text.ToUpper().Contains("DROP") ||
                textBoxQueryString.Text.ToUpper().Contains("TRUNCATE") ||
                textBoxQueryString.Text.ToUpper().Contains("UPDATE")  ||
                !textBoxQueryString.Text.ToUpper().Contains("SELECT"))
            {
                MessageBox.Show("Only SELECT query statements are permitted.  Please edit the query string and try again.", 
                    "Invalid SQL entries.....", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                return false;
            }
            return true;
        }
    }
}
